package commEntity

import (
	"encoding/json"
)

type RepoCompEntity struct {
	BaseEntity *BaseEntity // 基本信息

	CompRepo string // 设备厂商名称
	CompType string // 设备类型名
	CompName string // 设备名称

	CompParam map[string]interface{} // 配置参数
}

type repoCompPo struct {
	Id         int64                  `json:"id" db:"id"`                  // 对象id
	CreateTime int64                  `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64                  `json:"updateTime" db:"update_time"` // 更新时间
	CompRepo   string                 `json:"compRepo" db:"comp_repo"`     // 设备厂商名称
	CompType   string                 `json:"compType" db:"comp_type"`     // 设备类型名
	CompName   string                 `json:"compName" db:"comp_name"`     // 设备名称
	CompParam  map[string]interface{} `json:"compParam" db:"comp_param"`   // 配置参数
}

func (e *RepoCompEntity) Type() string {
	return "RepoCompEntity"
}

func (e *RepoCompEntity) TableName() string {
	return "tb_repo_comp"
}

func (e *RepoCompEntity) GetBaseEntity() *BaseEntity {
	return e.BaseEntity
}

func (e *RepoCompEntity) Instance() *RepoCompEntity {
	return &RepoCompEntity{
		BaseEntity: new(BaseEntity),
		CompParam:  make(map[string]interface{}),
	}
}

func (e *RepoCompEntity) NewEntity() IEntity {
	return e.Instance()
}

func (e *RepoCompEntity) NewPo() interface{} {
	return &repoCompPo{
		CompParam: make(map[string]interface{}),
	}
}

func (e *RepoCompEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return MakeServiceKey(p)
}

func (e *RepoCompEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.CompRepo)
	p = append(p, e.CompType)
	p = append(p, e.CompName)
	return p
}

func (e *RepoCompEntity) BuildByJson(jsn *string) error {
	var po repoCompPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *RepoCompEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *RepoCompEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*repoCompPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.CompParam = po.CompParam
	ptr.CompRepo = po.CompRepo
	ptr.CompType = po.CompType
	ptr.CompName = po.CompName
}

func (e *RepoCompEntity) BuildToPo() interface{} {
	ptr := e
	po := repoCompPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.CompParam = ptr.CompParam
	po.CompRepo = ptr.CompRepo
	po.CompType = ptr.CompType
	po.CompName = ptr.CompName

	return &po
}
